package com.xxgc.mybatis.dao;

import com.xxgc.mybatis.po.User;
import com.xxgc.mybatis.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test {
    //单元测试
    //不需要启动整个工程，只需要加载测试所依赖的模块
    @Test
    public void test(){
        //调用工具类 得到sqlSession
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //加载dao层映射
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        //通过调用接口执行语句
        List<User> users = mapper.selectAllUsers();
        //循环打印内容
        for (User user : users) {
            System.out.println("user = " + user);
        }
        //手动关闭sqlsession(学到了spring过后就不需要了)
        sqlSession.close();
    }
    @Test
    public void test1(){
        //调用工具类 得到sqlSession
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //加载dao层映射
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        //通过调用接口执行语句
        User user = mapper.selectById(1);


        System.out.println("user = " + user);
        //手动关闭sqlsession(学到了spring过后就不需要了)
        sqlSession.close();
    }
    @Test
    public void test6(){
        //调用工具类 得到sqlSession
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //加载dao层映射
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        //通过调用接口执行语句
        List<User> users = mapper.selectUsersLike("三");
        //循环打印内容
        for (User user : users) {
            System.out.println("user = " + user);
        }
        sqlSession.close();
    }
    @Test
    public void test2(){
        //调用工具类 得到sqlSession
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //加载dao层映射
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        //通过调用接口执行语句

       int i= mapper.addUser(  new User("qqq","123"));

        System.out.println("i = " + i);
        sqlSession.commit();
        //手动关闭sqlsession(学到了spring过后就不需要了)
        sqlSession.close();
    }
    @Test
    public void test3(){
        //调用工具类 得到sqlSession
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //加载dao层映射
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        //通过调用接口执行语句

        int i = mapper.updateUser(1);
        System.out.println("i = " + i);

        sqlSession.commit();
        //手动关闭sqlsession(学到了spring过后就不需要了)
        sqlSession.close();
    }
    @Test
    public void test4(){
        //调用工具类 得到sqlSession
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //加载dao层映射
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        //通过调用接口执行语句

        int i = mapper.deleteUserById(1);
        System.out.println("i = " + i);

        sqlSession.commit();
        //手动关闭sqlsession(学到了spring过后就不需要了)
        sqlSession.close();
    }
    @Test
    public void test5(){
        //调用工具类 得到sqlSession
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //加载dao层映射
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        //通过调用接口执行语句
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("myname","张三");
        map.put("mypass","123456");
        int i = mapper.addUserMap(map);
        System.out.println("i = " + i);
        sqlSession.commit();
        //手动关闭sqlsession(学到了spring过后就不需要了)
        sqlSession.close();
    }
}